<?php

namespace app\api\validate;

use think\Exception;
use think\Request;
use think\Validate;

class BaseValidate extends Validate
{
    /**
     * @return true
     * @throws Exception
     */
    public function goCheck()
    {
        $request = Request::instance();
        $data = $request->param();
        $result = $this->batch()->check($data);
        if (!$result) {
            foreach ($this->error as $r) {
                throw new Exception($r);

            }
        } else {
            return true;
        }
    }

    /**
     * 校验是否是正整数
     * @param $value
     * @param $rule
     * @param $data
     * @param $field
     * @return string|true
     */
    protected function isPositiveInteger($value, $rule = "", $data = "", $field = "")
    {
        if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0) {
            return true;
        } else {
            return false;
        }
    }


    protected function isMobile($value, $rule = "", $data = "", $field = "")
    {

        if (empty($value) || !Validate::regex($value, "^1\d{10}$")) {
            return false;
        } else {
            return true;
        }

    }

    /**
     * 是否为空
     * @param $value
     * @param $rule
     * @param $data
     * @param $field
     * @return bool
     */
    protected function isNotEmpty($value, $rule = "", $data = "", $field = "")
    {
        if (empty($value)) {
            return false;
        } else {
            return true;
        }
    }
}